<template>
  <div class="main">
    <section class="todoapp">
      <TodoHeader :arr="list" @add="addFn"></TodoHeader>
      <TodoMain :arr="showList" @del="delFn"></TodoMain>
      <TodoFooter
        :arr="list"
        @clearFn="clearDone"
        @changeType="changeTypeFn"
      ></TodoFooter>
    </section>
  </div>
</template>

<script>
// 样式引入 + 导入组件并注册使用
import "./styles/base.css";
import "./styles/index.css";
import "bootstrap/dist/css/bootstrap.css";
import TodoHeader from "@/components/TodoHeader.vue";
import TodoMain from "@/components/TodoMain.vue";
import TodoFooter from "@/components/TodoFooter.vue";

export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  watch: {
    list: {
      deep: true,
      handler(newVal) {
        localStorage.setItem(JSON.stringify("todos", newVal));
      },
    },
  },
  data() {
    return {
      type: "",
      list: JSON.parse(localStorage.getItem("todos")) || [],
      //   list: [
      //     {
      //       id: 100,
      //       name: "吃饭",
      //       isDone: true,
      //     },
      //     {
      //       id: 201,
      //       name: "睡觉",
      //       isDone: false,
      //     },
      //     {
      //       id: 103,
      //       name: "打豆豆",
      //       isDone: true,
      //     },
      //   ],
    };
  },
  computed: {
    showList() {
      if (this.type === "active") {
        return this.list.filter((item) => !item.isDone);
      } else if (this.type === "ok") {
        return this.list.filter((item) => item.isDone);
      } else {
        return this.list;
      }
    },
  },
  methods: {
    changeTypeFn(type) {
      this.type = type;
    },
    clearDone() {
      this.list = this.list.filter((item) => !item.isDone);
    },
    addFn(name) {
      this.list.unshift({
        id: +new Date(),
        name,
        isDone: false,
      });
    },
    delFn(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
  },
};
</script>

<style>
.main {
  margin-left: 20px;
}
.todoapp {
  padding-top: 45px;
  padding-bottom: 50px;
}
</style>
